package cn.formal.controller;

import cn.formal.entity.Result;
import cn.formal.service.SearchService;
import cn.formal.vi.QuestionAnswer;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("system")
public class SystemController {
    @Autowired
    private SearchService searchService;

    /*
        添加问答
     */
    @ApiOperation("添加问题的接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "answer", value = "问题和答案")
    })
    @PostMapping("addQuestion")
    public Result addQuestion(@RequestBody QuestionAnswer answer){
        log.info("[addQuestion接收前端信息] - answer: {}",answer);
        return searchService.addDoc(answer);
    }

    /*
        根据id查询问答
     */
    @ApiOperation("根据id查询问答的接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "问答id")
    })
    @GetMapping("findById")
    public Result findById(Long id){
        log.info("[findById接收前端信息] - id: {}",id);
        return searchService.findById(id);
    }
    /*
        根据id修改问答
     */
    @ApiOperation("修改问答的接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "answer", value = "问答id、问题和答案")
    })
    @PostMapping("updateById")
    public Result updateById(@RequestBody QuestionAnswer answer){
        log.info("[updateById接收前端信息] - answer: {}",answer);
        return searchService.update(answer);
    }

    /*
        根据id删除问答
     */
    @ApiOperation("根据id删除问答的接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "问答id")
    })
    @GetMapping("deleteById")
    public Result deleteById(Long id){
        log.info("[deleteById接收前端信息] - id: {}",id);
        return searchService.deleteById(id);
    }

    /*
        删除所有问答
     */
    @ApiOperation("删除所有问答的接口")
    @GetMapping("deleteAll")
    public Result deleteAll(){
        return searchService.deleteAll();
    }

    /*
        根据问题(title)模糊查询
     */
    @ApiOperation(value = "根据title查找符合条件的问答")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "title",value = "问题/问题关键词")
    })
    @GetMapping("findByTitle")
    public Result findByTitle(String title){
        log.info("[findByTitle接收前端信息] - title: {}",title);
        return searchService.findByTitle(title);
    }

    /*
        分页查询所有问答
     */
    @ApiOperation(value = "分页查询所有问答")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "查询第几页"),
            @ApiImplicitParam(name = "size",value = "单页问答条数")
    })
    @GetMapping("findByPage")
    public Result findByPage(Integer pageNum,Integer size){
        log.info("[findByPage接收前端信息] - pageNum: {},size: {}",pageNum,size);
        if (pageNum - 1 < 0){
            pageNum = 0;
        }else {
            pageNum = pageNum -1;
        }
        return searchService.findAll(pageNum,size);
    }
}
